接續昨天的分享,今天我們來繼續跟大家分享Spring security這個強大的模組吧!
其實我們昨天在安裝完套件後,我們輸入 「http://localhost:8080/login」
,我們會看到預設的身分登入畫面👇
所以接著下來,我們就順勢來實現身分驗證的機制吧!
UserDetails
來存放請求的使用資訊@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "_user")
public class UserInfo implements UserDetails {
@Id
@GeneratedValue
private Long id;
@Column(unique = true)
private String email;
private String password;
private String userName;
@Enumerated(EnumType.STRING)
private UserRole userRole;
public UserInfo(String userName, String email, String password) {
this.userName = userName;
this.email = email;
this.password = password;
}
@Override
public List<SimpleGrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> authorities =
Arrays.stream(userRole.name().split(",")).map(
SimpleGrantedAuthority::new).collect(Collectors.toList());
return authorities;
}
@Override
public String getUsername() {
return email;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
這邊我們可以把UserRole
設定為以下三種權限
public enum UserRole {
ADMIN, USER, GUEST;
}
我們明天接著繼續來實現身分驗證吧 !!!!